package com.zhouxl.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 执行方法所消耗的时间
 * @author zhouxl
 * @DATE 2021/7/9
 * @descriptoin
 */
@Component
@Aspect
public class ExecMethodTimeAop {

    private Logger logger = LoggerFactory.getLogger(this.getClass().getName());

    // 耗费
    @Around("@annotation(com.zhouxl.aop.TimeCostAnnotation)")
    public Object totalInvokeMethodTime(ProceedingJoinPoint pjp) throws Throwable {
        long startTime = System.currentTimeMillis();
        logger.info("开始时间:" + new Date());
        Object proceed = pjp.proceed();
        logger.info("结束时间:" + new Date());
        long endTime = System.currentTimeMillis();
        logger.info("耗时:"+(endTime-startTime));
        return proceed;
    }

}
